<template>
	<view>
		<!-- tab栏切换 -->
		<view class="tab_w flex_a bf" v-if="!cardNo">
			<block v-for="item in tabList" :key="item.status">
				<view class="flex1 h80 lh80 border-r4 ft30 tac" :class="activeTab == item.status ? 'b-primary cf' : ''"
					@click="tapTab(item.status)">{{ item.name }}</view>
			</block>
		</view>
		<view class="tab_w flex_a " v-else>
			<text class="plr30">卡号：{{ cardNo }}</text>
		</view>
		<view class="plr30 pt150">
			<block v-for="item in loadList" :key="item.id">
				<!-- 充电 start -->
				<view class="flex_a mb30 p30 bf border-r1" v-if="activeTab == 1"
					@click="routerLink({ path: '/pages/user/myOrder/chargeDetail', query: { orderNo: item.orderNo } })">
					<view class="w140 w130">
						<image src="/static/cd_img.png" mode="widthFix"></image>
					</view>
					<view class="flex1 pl20">
						<view>订单编号： {{ item.orderNo }}</view>
						<view class="flex_sb h60">
							<text class="warning">￥{{ item.actuallyPay }}</text>
							<text
								:class="item.status == 0 ? 'warning' : item.status == 1 ? 'green' : item.status == 2 ? 'danger' : ''">
								{{
									item.status == 0
										? '进行中'
										: item.status == 1
										? '已完成'
										: item.status == 2
										? '待支付'
										: item.status == 3
										? '已退款'
										: item.status == 4
										? '已取消'
										: '--'
								}}
							</text>
						</view>
						<view class="c6 ft26">开始充电时间：{{ item.stime   }}</view>

					</view>
				</view>

				<!-- 充电 end -->

				<!-- 租车 start -->
				<view class="flex_a mb30 p30 bf border-r1" v-if="activeTab == 2"
					@click="routerLink({ path: '/pages/user/myOrder/rentCarDetail', query: { orderNo: item.orderNo } })">
					<view class="w140 w130">
						<image src="/static/zc_img.png" mode="widthFix"></image>
					</view>
					<view class="flex1 pl20 ellipsis">
						<view class="ellipsis">{{ item.siteName }}</view>
						<view class="lh50 ft26">车辆编号：{{ item.deviceNo }}</view>
						<view class="c6">有效期：{{ item.mealTime  }}</view>
					</view>
					<view class="tae ft24" style="max-width: 120upx;"
						:class="item.status == 0 ? 'warning' : item.status == 2 ? 'danger' : item.status == 1 ? 'green' : ''">
						{{ item.status == 0 ? '租赁中' : item.status == 2 ? '已逾期，请及时归还车辆' : item.status == 1 ? '已完成' : '--' }}
					</view>
				</view>
				<!-- 租车 end -->
				<!-- 换电 start -->
				<view class="flex_a mb30 p30 bf border-r1" v-if="activeTab == 3"
					@click="routerLink({ path: '/pages/user/myOrder/rentEleDetail', query: { orderNo: item.orderNo } })">
					<view class="w140 w130">
						<image src="/static/hd_img.png" mode="widthFix"></image>
					</view>
					<view class="flex1 plr20">
						<view class="">{{ item.siteName }}</view>
						<view class="lh50 ft26">电池编号：{{ item.newBatteryNo || '--' }}</view>
						<view class="c6">
							有效期：
							<text class="ft24">{{ item.startTime  }} 至 {{ item.mealTime  }}</text>
						</view>
					</view>
					<view class="tae whn" style="max-width: 120upx;"
						:class="item.status == 0 ? 'warning' : item.status == 1 ? 'green' : item.status == 2 ? 'danger' : ''">
						{{ item.status == 0 ? '进行中' : item.status == 1 ? '已完成' : item.status == 2 ? '已逾期，请及时归还电池' : '--' }}
					</view>
				</view>
				<!-- 换电 end -->
			</block>
			<view class="flex_aj column ptb20" v-if="showTime && loadList.length == 0">
				<view class="c9 pt20">您还没有相关订单~</view>
			</view>
			<uni-load-more v-if="showTime && loadList.length > 0" :status="loadMoreText" color="#999"></uni-load-more>
		</view>
	</view>
</template>

<script>
	import loadMore from '@/mixins/loadMore'
	import {
		exchangeOrder,
		orderCharge,
		rentCarOrder
	} from '@/api/user.js'
	export default {
		name: 'myOrder',
		mixins: [loadMore],
		data() {
			return {
				showTime: false,
				activeTab: 1, // 1 充电 2 租车 3 换电
				tabList: [{
					name: '充电',
					status: 1
				}, {
					name: '租车',
					status: 2
				}, {
					name: '换电',
					status: 3
				}], // 头部tab栏
				cardNo: '' // ic卡号
			}
		},
		onLoad(e) {

			this.activeTab = e.activeTab || 1
			if (e.cardNo) {
				this.cardNo = e.cardNo || ''
				uni.setNavigationBarTitle({
					title: '充电记录'
				})
			}
		},
		onShow() {},
		onHide() {},
		computed: {
			isLogin() {
				return this.$store.state.user.isLogin
			}
		},
		methods: {

			// 点击tab栏
			tapTab(status) {
				this.activeTab = status
				this.page == 1
				this.loadList = []
				this.loadData()
			},
			async loadData() {
				// 充电订单
				if (this.activeTab == 1) {
					this.orderCharge()
				}
				// 租车订单
				if (this.activeTab == 2) {
					this.rentCarOrder()
				}
				// 换电订单
				if (this.activeTab == 3) {
					this.exchangeOrder()
				}
			},
			// 换电订单
			async exchangeOrder() {
				const res = await exchangeOrder({
					pageNum: this.page,
					pageSize: this.limit
				})
				this.showTime = true
				this.loadList = this.page == 1 ? res.rows : [...this.loadList, ...res.rows]

				this.pageLength = res.rows.length
				this.loadMoreText = res.rows.length < this.limit ? 'noMore' : 'more'
			},
			// 充电订单
			async orderCharge() {
				const res = await orderCharge({
					pageNum: this.page,
					pageSize: this.limit,
					cardNo: this.cardNo
				})
				this.showTime = true
				this.loadList = this.page == 1 ? res.rows : [...this.loadList, ...res.rows]

				this.pageLength = res.rows.length
				this.loadMoreText = res.rows.length < this.limit ? 'noMore' : 'more'
			},
			// 租车订单
			async rentCarOrder() {
				const res = await rentCarOrder({
					pageNum: this.page,
					pageSize: this.limit
				})
				this.showTime = true
				this.loadList = this.page == 1 ? res.rows : [...this.loadList, ...res.rows]
				console.log('this.loadList--', this.loadList);
				this.pageLength = res.rows.length
				this.loadMoreText = res.rows.length < this.limit ? 'noMore' : 'more'
			}
		}
	}
</script>

<style scoped lang="scss">
	.tab_w {
		position: fixed;
		top: 16upx;
		left: 30upx;
		width: 690upx;
		height: 100upx;
		padding: 10upx 14upx;
		box-sizing: border-box;
		background-color: #fff;
		border-radius: 50upx;
		overflow: hidden;
		box-shadow: 0px 4px 18px 0px rgba(50, 130, 255, 0.24);
	}
</style>
